package com.itheima.service.impl;

import com.itheima.dao.UserDap;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class UserServiceImpl implements UserService {
    @Override
    public int regist(User user) throws IOException {
        //1 加载mybatis的核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfing.xml");
        //2 创建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3 通过SqlSessionFactory对象获取SqlSession对象
        SqlSession sqlSession = factory.openSession(true);
        //4 获取UserDao的代理对象
        UserDap userDao = sqlSession.getMapper(UserDap.class);
        //5 执行方法,得到结果
        int count=userDao.addUser(user);
        //6 释放资源,返回结果
        sqlSession.close();
        return count;
    }
}
